package edu.chc.hhsh.ros.ros_order_apply;

import com.pub.database.*;
import javax.servlet.http.*;
import bsh.*;

public class Action {
    QueryBean gqbnDAO=null;
    public String action(javax.servlet.jsp.PageContext pageContext){
       HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
       HttpServletResponse response=(HttpServletResponse)pageContext.getResponse();

       View view=(View)request.getAttribute("view");
//       (new WebFunction()).setPortal_to_session(request); //set session from portal
//       if (request.getSession().getAttribute("u_id")==null) {
//          try{
//             request.getRequestDispatcher((new StringBuffer("login.jsp")).insert(0,"/").toString()).forward(request,response);
//             return "login";
//          }
//          catch(Exception le){
//             view.setOnloadScript("can not redirect to login page...please contact IT");
//             return "";
//          }
//       }

       response.setHeader("Cache.Control","no-cache");
       response.setHeader("Pragma","no-cache");
       response.setDateHeader("Expires",0);

//       view.setLoginUserNO(request.getSession().getAttribute("u_id").toString());
       view.setLoginUserNO("05227");
       gqbnDAO=new QueryBean(view.getDBAlias(),view.getDBDebug(),view.getSourceCharset(),view.getTargetCharset());
       Business business=new Business(gqbnDAO);

       /* 這段為每支Action.java 唯一會有差異的地方 */
       DataView DataView= new DataView(gqbnDAO);
       DataViewForm DataViewForm = new DataViewForm();

       request.setAttribute("DataViewForm",DataViewForm);

       //Flow flow=new Flow(gqbnDAO,"AMS",view,business);

       view.setDataView(DataView);
       view.setDataViewForm(DataViewForm);
       //view.setFlow(flow);
       /***************************************/

       String lstrResult=null;
       Interpreter lbhsIPR = new Interpreter();//可動態執行字串中的程式碼物件
       StringBuffer lstbDynamicCode=new StringBuffer();

       try {
           //需先定義字串中的物件變數
           lbhsIPR.set("business",business);
           lbhsIPR.set("view", view);

           if(view.getPrgAction()!=null && view.getPrgAction().equals("getData")){//利用AJAX即時讀取資料
               System.out.println(view.getKeywords());
               lstrResult=business.getData(view);

               pageContext.getOut().clear();
               pageContext.getOut().write(lstrResult);
               pageContext.getOut().flush();
               return "login";
            }

            //每次submit一開始都要做的事
            lstrResult = business.actionForInit(view);

            //執行各動作對應的method
            if(view.getPrgAction()!=null){
                lstbDynamicCode.append("business.actionFor");
                lstbDynamicCode.append(view.getPrgAction().substring(0,1).toUpperCase());
                lstbDynamicCode.append(view.getPrgAction().substring(1));
                lstbDynamicCode.append("(view);");
                //執行字串中的動態程式碼
                lstrResult=lbhsIPR.eval(lstbDynamicCode.toString()).toString();
            }

            lstrResult = business.actionForEnd(view);

            business.setButtonShow(view,view.getPrgAction(),view.getPrgStatus());
            business.setReadonlyFields(view,view.getPrgAction(),view.getPrgStatus());
            business.setShowFields(view,view.getPrgAction(),view.getPrgStatus());

            return "";
       }
       catch(Exception e){
          view.setOnloadScript("alert('" + e.toString().replaceAll("'","′") + ", please contact IT.');");
          return "";
       }
       finally{
          try{
             if(gqbnDAO!=null){
                gqbnDAO.close();
             }
          }
          catch(Exception e){
             System.out.println("can not close querybean in Action.java");
          }
       }
   }
}
